﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace ButtonTree2
{
    public class OneLineText : ILineText
    {
        public OneLineText()
        {
            _strFmtText = new StringFormat();
            _strFmtText.Alignment = StringAlignment.Center;
            _strFmtText.LineAlignment = StringAlignment.Center;
            _strFmtText.FormatFlags = StringFormatFlags.NoWrap;
        }

        private StringFormat _strFmtText;

        private Size _size;

        // 获取合适的大小
        public Size GetHintSize()
        {
            return _size;
        }

        private string _text;
        private bool _bTextChanged = false;

        public string Text
        {
            get
            {
                return _text;
            }
            set
            {
                if (_text != value)
                {
                    _text = value;
                    _bTextChanged = true;
                }
            }
        }


        private void CalcHintSize(Graphics g, Font font)
        {
            SizeF sizeF = g.MeasureString(_text, font);
            _size = new Size((int)sizeF.Width + 1, (int)sizeF.Height + 1);
        }


        // 更新双行文本的宽高，在调用GetHintSize前应调用此函数
        public void UpdateTextSize(Graphics g, Font font)
        {
            if (_bTextChanged)
            {
                // 若文字改变，需要计算一遍Size
                CalcHintSize(g, font);
                _bTextChanged = false;
            }
        }

        private static PointF GetTextCenterPoint(Rectangle centerRect)
        {
            return new PointF(
                (centerRect.Left + centerRect.Right) / 2f,
                (centerRect.Top + centerRect.Bottom) / 2f + 1f  // DrawString 未能对文本进行完全的纵向居中对齐，因此增加偏移
                );
        }

        // 绘制
        public void Draw(Graphics g, Font font, Brush brush, Rectangle rect)
        {
            if (!String.IsNullOrEmpty(_text))
            {
                UpdateTextSize(g, font);

                int left = rect.Left + (rect.Width - _size.Width) / 2;
                int top = rect.Top + (rect.Height - _size.Height) / 2;

                Rectangle rectCenterText = new Rectangle(left, top, _size.Width, _size.Height);

                //g.DrawRectangle(Pens.Blue, rectCenterText);

                PointF center = GetTextCenterPoint(rectCenterText);

                g.DrawString(_text, font, brush, center.X, center.Y, _strFmtText);
            }
        }

        //++ 2012.8.28 新增绘制
        public void Draw(Graphics g, Font font)
        {
            PointF center = GetTextCenterPoint(Area);

//             float centerX = (Area.Left + Area.Right) / 2f;
//             float centerY = (Area.Top + Area.Bottom) / 2f + 1f;  // DrawString 未能对文本进行完全的纵向居中对齐，因此增加偏移

            using (Brush brush = new SolidBrush(TextColor))
            {
                g.DrawString(_text, font, brush, center.X, center.Y, _strFmtText);
            }
        }

        private Color _textColor = Color.Black;
        
        public Color TextColor
        {
            get
            {
                return _textColor;
            }
            set
            {
                _textColor = value;
            }
        }

        //--

        #region ILayout 成员
        void ILayout.AdjustSize()
        {
            
        }

        public Rectangle Area
        {
            get;
            set;
        }

        void ILayout.AdjustPosition(Rectangle rectArea)
        {
            // 2012.8.28 记录位置
            Area = rectArea;
        }

        #endregion

        /*
        //++ 2012-10-22 Added by Ant
        public StringAlignment Alignment
        {
            get;
            set;
        }

        public StringAlignment LineAlignment
        {
            get;
            set;
        }

        public void DrawWithAlign(Graphics g, Font font)
        {
            DrawWithAligh(g, font, Alignment, LineAlignment);
        }

        public void DrawWithAligh(Graphics g, Font font, StringAlignment align, StringAlignment lineAlign)
        {
            StringFormat sf = new StringFormat();
            sf.Alignment = align;
            sf.LineAlignment = lineAlign;

            using (Brush brush = new SolidBrush(TextColor))
            {
                g.DrawString(_text, font, brush, Area, _strFmtText);
            }
        }
        //--
        */
    }
}
